import unittest
from time import sleep

import requests
from api.RegLoginApi import RegLoginApi
from common import utils_p2p
from common.utils_p2p import clear_user_info
from parameterized import parameterized


class TestRegLogin(unittest.TestCase):
    # 类属性
    reg_login_api = None
    session = requests.Session()

    @classmethod
    def setUpClass(cls) -> None:
        print('类前置,数据初始化')
        clear_user_info()
        cls.reg_login_api = RegLoginApi(cls.session)

    @classmethod
    def tearDownClass(cls) -> None:
        print('类后置,关闭session对象')
        cls.session.close()

    @parameterized.expand(utils_p2p.read_json("test_data", "img_verify_code"))
    def test01_img_verify_code(self, r, exp_status_code):
        # 获取图片验证码
        response = self.reg_login_api.get_img_verify_code(r)
        print(f"获取图片验证码结果为：{response}")
        self.assertEqual(exp_status_code, response.status_code)

    # 获取短信验证码测试用例
    @parameterized.expand(utils_p2p.read_json("test_data", "phone_verify_code"))
    def test02_phone_verify_code(self, r, phone, imgVerifyCode, exp_status_code, exp_status, exp_data):
        # 获取短信验证码
        self.reg_login_api.get_img_verify_code(r)
        response = self.reg_login_api.get_phone_verify_code(phone_v=phone, imgVerifyCode_v=imgVerifyCode, type_v='reg')
        print(f"获取短信验证码结果为：{response}")
        utils_p2p.common_assert(self, response, exp_status_code, exp_status, exp_data)
        # 断言
        # self.assertEqual(200, response.status_code)
        # self.assertEqual(200, resp_dict.get('status'))
        # self.assertIn('发送成功', resp_dict.get('description'))

    # 注册测试用例
    @parameterized.expand(utils_p2p.read_json("test_data", "user_register"))
    def test03_register(self, r, phone, password, imgVerifyCode, phone_code, dy_server, exp_status_code, exp_status,
                        exp_data):
        # 获取图片验证码的
        self.reg_login_api.get_img_verify_code(r)
        # 获取短信验证码的
        self.reg_login_api.get_phone_verify_code(phone_v=phone, imgVerifyCode_v=imgVerifyCode, type_v='reg')
        # 注册
        response = self.reg_login_api.user_register(phone_v=phone, password_v=password, verifycode_v=imgVerifyCode,
                                                    phone_code_v=phone_code, dy_server=dy_server)
        resp_dict = response.json()
        print(f"注册结果为：{resp_dict}")
        # 断言
        utils_p2p.common_assert(self, response, exp_status_code, exp_status, exp_data)
        # self.assertEqual(200, response.status_code)
        # self.assertEqual(200, resp_dict.get('status'))
        # self.assertIn('注册成功', resp_dict.get('description'))

    # 登录测试用例
    @parameterized.expand(utils_p2p.read_json("test_data", "user_login"))
    def test04_user_login(self, keywords, password, exp_status_code, exp_status, exp_data):
        response = self.reg_login_api.user_login(keywords_v=keywords, password_v=password)
        resp_dict = response.json()
        print(f"登录结果为：{resp_dict}")
        # 断言
        utils_p2p.common_assert(self, response, exp_status_code, exp_status, exp_data)
        # self.assertEqual(200, response.status_code)
        # self.assertIn('登录成功', resp_dict.get('description'))


    # def test05_error_pwd_wait(self):
    #     # 密码错误1次
    #     response = self.reg_login_api.user_login("13555545448", "123123")
    #     # 断言
    #     utils_p2p.common_assert(self, response, 200, 100, "密码错误1次")
    #     # 密码错误2次
    #     response = self.reg_login_api.user_login("13555545448", "123123")
    #     # 断言
    #     utils_p2p.common_assert(self, response, 200, 100, "密码错误2次")
    #     # 密码错误3次
    #     response = self.reg_login_api.user_login("13555545448", "123123")
    #     # 断言
    #     utils_p2p.common_assert(self, response, 200, 100, "由于连续输入错误密码达到上限")
    #     # 等待60s
    #     sleep(60)
    #     # 登陆成功
    #     response = self.reg_login_api.user_login("13555545448", "123abc")
    #     # 断言
    #     utils_p2p.common_assert(self, response, 200, 200, "登录成功")

